package com.ruoyi.project.pro.manager.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.pro.manager.domain.ProManager;
import com.ruoyi.project.pro.manager.domain.ProManagerVo;
import com.ruoyi.project.pro.manager.mapper.ProManagerMapper;
import com.ruoyi.project.pro.manager.service.IProManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: zhizun
 * @author: Mrs.ye
 * @create: 2020-09-17 16:34
 * @description: 产品管理的控制层接口的实现类
 **/
@Service
public class ProManagerServiceImpl implements IProManagerService {
    @Autowired
    private ProManagerMapper proManagerMapper;

    @Override
    public List<ProManagerVo> selectManagerList(ProManager proManager) {

        List<ProManager> proManagers = proManagerMapper.selectManagerList(proManager);
        List<ProManagerVo> proManagerVos = new ArrayList<>();
        /*
         * 给proManagerVos赋值
         * */
        for (ProManager manager : proManagers) {
            ProManagerVo proManagerVo = new ProManagerVo();
            Long lineId = manager.getLineId();
            Long sizeId = manager.getSizeId();
            Long styleId = manager.getStyleId();
            Long textureId = manager.getTextureId();
            Long pictureId = manager.getPictureId();
            String picturePath = proManagerMapper.selectPictureById(pictureId);
            String lineName = proManagerMapper.selectLineNameById(lineId);
            String sizeName = proManagerMapper.selectSizeNameById(sizeId);
            String styleName = proManagerMapper.selectStyleNameById(styleId);
            String textureName = proManagerMapper.selectTextureNameById(textureId);
            BeanUtils.copyBeanProp(proManagerVo, manager);
            proManagerVo.setLineName(lineName);
            proManagerVo.setSizeName(sizeName);
            proManagerVo.setStyleName(styleName);
            proManagerVo.setTextureName(textureName);
            proManagerVo.setPicturePath(picturePath);
            proManagerVos.add(proManagerVo);

        }
        return proManagerVos;


    }

    @Override
    public int insertPro(ProManager proManager) {
        checkManagerNameUnique(proManager);
        proManager.setCreateBy(ShiroUtils.getLoginName());
        return proManagerMapper.insertPro(proManager);
    }

    /**
     * 通过id产品,用于数据回显
     *
     * @param managerId
     * @return
     */
    @Override
    public ProManager selectProById(Long managerId) {
        ProManager proManager = proManagerMapper.selectProById(managerId);

        System.out.println("我是service" + proManager);
        return proManager;
    }

    @Override
    public String checkManagerNameUnique(ProManager proManager) {
        Long ManagerId = StringUtils.isNull(proManager.getManagerId()) ? -1L : proManager.getManagerId();
        ProManager info = proManagerMapper.checkManagerNameUnique(proManager.getManagerName());
        //如果根据公告名称查询出来了公告,并且查询出来的公告id和原本的id不同,表示同一个公告名称有不同的id,
        //反过来就是说不同的id相同的公告名称,这样公告名称就不唯一了
        if (StringUtils.isNotNull(info) && info.getManagerId().longValue() != ManagerId.longValue()) {
            return UserConstants.TYPE_CODE_NOT_UNIQUE;
        }
        return UserConstants.TYPE_CODE_UNIQUE;
    }

    @Override
    public int updatePro(ProManager proManager) {
        proManager.setUpdateBy(ShiroUtils.getLoginName());
        return proManagerMapper.updatePro(proManager);
    }

    @Override
    public int removeProById(Long[] ids) {

        return proManagerMapper.removePro(ids);
    }

    /**
     * 查看图片收藏夹
     *
     * @return
     */
    @Override
    public List<ProManagerVo> selectManagerFavList() {
        List<ProManager> proManagers = proManagerMapper.selectFavPro();
        List<ProManagerVo> proManagerVos = new ArrayList<>();
        System.out.println("我是收藏夹" + proManagers);
        /*
         * 给proManagerVos赋值
         * */
        for (ProManager manager : proManagers) {
            ProManagerVo proManagerVo = new ProManagerVo();
            Long lineId = manager.getLineId();
            Long sizeId = manager.getSizeId();
            Long styleId = manager.getStyleId();
            Long textureId = manager.getTextureId();
            Long pictureId = manager.getPictureId();
            String picturePath = proManagerMapper.selectPictureById(pictureId);
            String lineName = proManagerMapper.selectLineNameById(lineId);
            String sizeName = proManagerMapper.selectSizeNameById(sizeId);
            String styleName = proManagerMapper.selectStyleNameById(styleId);
            String textureName = proManagerMapper.selectTextureNameById(textureId);
            BeanUtils.copyBeanProp(proManagerVo, manager);
            proManagerVo.setLineName(lineName);
            proManagerVo.setSizeName(sizeName);
            proManagerVo.setStyleName(styleName);
            proManagerVo.setTextureName(textureName);
            proManagerVo.setPicturePath(picturePath);
            proManagerVos.add(proManagerVo);

        }
        return proManagerVos;
    }

    @Override
    public int updateStatusById(Long pictureId) {


        return   proManagerMapper.updateProById(pictureId);
}

    @Override
    public int updateStatusByManagerId(Long managerId) {
        return proManagerMapper.updateProByManagerId(managerId);
    }


}
